package com.woniuxy.business.config;

import com.rabbitmq.client.Channel;
import com.woniuxy.business.dao.CustomerCouponDao;
import com.woniuxy.business.util.WebSocketUtil;
import com.woniuxy.commons.entity.CustomerCoupon;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.websocket.Session;
import java.io.IOException;

@Component
public class SqlConsumer {
    @Resource
    private RedisTemplate<String,Integer> redisTemplate;
    @Resource
    private CustomerCouponDao customerCouponDao;

    @RabbitListener(queues = "sql_queue")
    public void receive(Channel channel, Message message) throws IOException {
        //解析消息
        String idString = new String(message.getBody(), "utf-8");
        String userId = idString.substring(idString.indexOf(":")+1);
        String couponId = idString.substring(0,idString.indexOf(":"));
        //存入数据库
        CustomerCoupon customerCoupon = new CustomerCoupon();
        customerCoupon.setCouponId(Integer.parseInt(couponId));
        customerCoupon.setCustomerId(Integer.parseInt(userId));
        customerCouponDao.addOne(customerCoupon);
        //手动确认消费消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}